package com.lgs.waiting;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by lgs on 2018/5/4.
 */

public class ProgressLoading extends DialogFragment {

    @SuppressLint("StaticFieldLeak")
    private static ProgressLoading instance;

    private TextView mTvMessage;

    public static ProgressLoading newInstance() {
        instance = new ProgressLoading();

        return instance;
    }

    private ProgressLoading() {
        super();
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);//设置取消标题栏
        setCancelable(false);//外围点击 不关闭
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.layout_dialog_progress, container, false);
        initView(dialogView);
        return dialogView;
    }

    private void initView(View dialogView) {
        ImageView mIvProgressSpinner = dialogView.findViewById(R.id.iv_progress_spinner);
//        ImageView mIvFailure = dialogView.findViewById(R.id.iv_failure);
//        ImageView mIvSuccess = dialogView.findViewById(R.id.iv_success);
        mTvMessage = dialogView.findViewById(R.id.tv_message);
        mIvProgressSpinner.setImageResource(R.drawable.round_spinner_fade);
//        AnimationDrawable animationDrawable = (AnimationDrawable) mIvProgressSpinner.getDrawable();
    }

    public ProgressLoading setMessage(String message) {
        mTvMessage.setText(message);
        return this;
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        if (!getActivity().isFinishing()) {
            super.show(manager, tag);
        } else {
            instance = null;
        }
    }

    @Override
    public void onDestroy() {
        instance = null;
        super.onDestroy();
    }

    private OnDialogDismiss onDialogDismiss;

    public OnDialogDismiss getOnDialogDismiss() {
        return onDialogDismiss;
    }

    public void setOnDialogDismiss(OnDialogDismiss onDialogDismiss) {
        this.onDialogDismiss = onDialogDismiss;
    }

    public interface OnDialogDismiss {
        void onDismiss();
    }
}
